<template>
  <div class="sidebar">
    <el-menu
        class="sidebar-el-menu"
        :default-active="onRoutes"
        :collapse="collapse"
        background-color="#324157"
        text-color="#bfcbd9"
        active-text-color="#20a0ff"
        unique-opened
        router
    >
      <template v-for="item in items">
        <template v-if="item.subs">
          <el-submenu :index="item.index" :key="item.index">
            <template slot="title">
              <i :class="item.icon"></i>
              <span>{{ item.title }}</span>
            </template>
            <template v-for="subItem in item.subs">
              <el-menu-item
                  :index="subItem.url"
                  :key="subItem.index"
              ><i :class="subItem.icon"></i>{{ subItem.title }}
              </el-menu-item>
            </template>
          </el-submenu>
        </template>
        <template v-else>
          <el-menu-item :index="item.url" :key="item.index">
            <i :class="item.icon"></i>
            <span>{{ item.title }}</span>
          </el-menu-item>
        </template>
      </template>
    </el-menu>
  </div>
</template>

<script>
import bus from '../assets/js/event-bus';

export default {
  data() {
    return {
      loginUser: {},
      collapse: false,
      items: [
        {
          icon: 'el-icon-s-home',
          index: '1',
          url: 'dash-board',
          title: '系统首页'
        },
        {
          icon: 'el-icon-user',
          index: '2',
          title: '用户管理',
          subs: [
            {
              icon: 'el-icon-document',
              index: '2-1',
              url: 'user-list',
              title: '用户列表'
            }
          ]
        },
        {
          icon: 'el-icon-school',
          index: '3',
          title: '体育场馆管理',
          subs: [
            {
              icon: 'el-icon-document',
              index: '3-1',
              url: 'hall-list',
              title: '体育场馆列表'
            }
          ]
        },
        {
          icon: 'el-icon-box',
          index: '4',
          title: '体育器材管理',
          subs: [
            {
              icon: 'el-icon-document',
              index: '4-1',
              url: 'equipment-list',
              title: '体育器材列表'
            }
          ]
        },
        {
          icon: 'el-icon-money',
          index: '5',
          title: '租借器材管理',
          subs: [
            {
              icon: 'el-icon-document',
              index: '5-1',
              url: 'rental-list',
              title: '租借器材列表'
            }
          ]
        },
        {
          icon: 'el-icon-alarm-clock',
          index: '6',
          title: '预约场馆管理',
          subs: [
            {
              icon: 'el-icon-document',
              index: '6-1',
              url: 'appointment-list',
              title: '预约场馆列表'
            }
          ]
        },
        {
          icon: 'el-icon-chat-line-round',
          index: '7',
          title: '公告管理',
          subs: [
            {
              icon: 'el-icon-document',
              index: '7-1',
              url: 'announce-list',
              title: '公告列表'
            }
          ]
        },
        {
          icon: 'el-icon-chat-line-square',
          index: '8',
          title: '统计',
          subs: [
            {
              icon: 'el-icon-document',
              index: '8-1',
              url: 'statistics',
              title: '统计'
            }
          ]
        }
      ]
    };
  },
  computed: {
    onRoutes() {
      return this.$route.path.replace('/', '');
    }
  },

  created() {

    this.getLoginUser();
    // 通过 Event Bus 进行组件间通信，来折叠侧边栏
    bus.$on('collapse', msg => {
      this.collapse = msg;
      bus.$emit('collapse-content', msg);
    });
  },
  methods: {
    getLoginUser() {
      this.$ajax.post("/user/getLoginUser", {token: Tool.getLoginUser()}).then((response) => {
        let resp = response.data;
        if (resp.code === 0) {
          if (resp.data) {
            this.loginUser = resp.data;
            console.log(this.items[0])
            if (this.loginUser.roleId === 1) {
              this.items[0].url = "dash-board1"
              console.log(this.items[0])
            }

          } else {
            this.$message.error(resp.msg);
            this.$router.push("/login");
          }
        }
      });
    },
  }
};
</script>

<style scoped>
.sidebar {
  display: block;
  position: absolute;
  left: 0;
  top: 70px;
  bottom: 0;
  overflow-y: scroll;
}

.sidebar::-webkit-scrollbar {
  width: 0;
}

.sidebar-el-menu:not(.el-menu--collapse) {
  width: 250px;
}

.sidebar > ul {
  height: 100%;
}
</style>
